<!--车站票款差异操作-->
<template>
  <div class="cheZhanPKCY Add el-card is-always-shadow el-card__body">
    <el-dialog
      title="车站票款差异操作"
      :visible.sync="dialogVisible"
      @close="closed"
      width="950px">
      <avue-form v-if="isReload" class="cheZhanPKCYForm" :option="option" v-model="formData" @submit="handleSubmit">

        <template slot-scope="scope" slot="lineCode">
          <el-select :disabled="showType==3" v-model="formData.lineCode" placeholder="请选择" style="width: 150px" @change="changeLine" clearable filterable>
            <el-option
              v-for="(item,index) in lineCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="stationCode">
          <el-select :disabled="showType==3" v-model="formData.stationCode" placeholder="请选择" style="width: 150px" clearable @change="changeStation" filterable>
            <el-option
              v-for="(item,index) in stationCodes"
              :key="index"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="conductorPersonName">
          <el-select :disabled="showType==3" v-model="formData.conductorPersonName" placeholder="请选择" style="width: 150px" @change="(v)=>{formData.conductorPersonName=v.nikename;formData.conductorJobNo=v.userCode}" clearable filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.nikename"
              :value="item">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="conductorJobNo">
          <el-select v-model="formData.conductorJobNo" placeholder="请选择" style="width: 150px" clearable :disabled="true"  filterable>
            <el-option
              v-for="(item,index) in conductorPersonNames"
              :key="index"
              :label="item.userCode"
              :value="item.userCode">
            </el-option>
          </el-select>
        </template>
        <template slot-scope="scope" slot="incomeTime">
          <el-date-picker v-model="formData.incomeTime" value-format="yyyy-MM-dd" :disabled="showType==3"></el-date-picker>
        </template>
        <template slot-scope="scope" slot="settleBeginDate">
          <el-date-picker v-model="formData.settleBeginDate" value-format="yyyy-MM-dd" :disabled="showType==3" @change="xianjinYingShouAllAmounts" ></el-date-picker>
        </template>
        <template slot-scope="scope" slot="settleEndDate">
          <el-date-picker v-model="formData.settleEndDate" value-format="yyyy-MM-dd" :disabled="showType==3" @change="xianjinYingShouAllAmounts" :pickerOptions="{
                          disabledDate(time) { return time.getTime() > Date.now();}, }"></el-date-picker>
        </template>
        <template slot-scope="scope" slot="cashPayAmount">
          <el-input-number :disabled="showType==3" v-model="formData.cashPayAmount" controls-position="right" :precision="2" @change="(v)=>{formData.diffAmount=v-formData.cashReceiptsAmount-formData.shortAmount}"></el-input-number>
        </template>
        <template slot-scope="scope" slot="cashReceiptsAmount">
          <el-input-number v-model="formData.cashReceiptsAmount" controls-position="right" :precision="2" :disabled="true"></el-input-number>
        </template>
        <template slot-scope="scope" slot="shortAmount">
          <el-input-number v-model="formData.shortAmount" controls-position="right" :precision="2" :disabled="true"></el-input-number>
        </template>
        <!-- <template slot-scope="scope" slot="stationReceiptsAmount">
          <el-input-number :disabled="showType==3" v-model="formData.stationReceiptsAmount" controls-position="right" :precision="2" @change="(v)=>{formData.diffAmount=formData.cashPayAmount-v-formData.shortAmount}"></el-input-number>
        </template> -->
        <template slot-scope="scope" slot="diffAmount">
          <el-input-number v-model="formData.diffAmount" controls-position="right" :precision="2" :disabled="showType==3"></el-input-number>
        </template>

        <template slot-scope="scope" slot="menuForm">
          <el-button type="primary" @click="closed">返回</el-button>
        </template>
      </avue-form>
    </el-dialog>
  </div>
</template>


<script>
  import {
    fenExYuangettoFixed,
    getgetStationCodes,
    getLineCodes,
    getSwPmDeviceOperationCodeList,
    qianxiangType,getSysUserToLS
  } from "../../../../../api/subwayRat/commonCheck";
  import moment from 'moment';
  import {
    addSwAuStationTicketDifference,
    getCheZhanPKCYoption, updateSwAuStationTicketDifference, xianjinYingShouAllAmount,xianjinYingShouAllAmounts
  } from "../../../../../api/subwayRat/conductorDifferences/conductorTicketChaYiShenhe/cheZhanPKCY/cheZhanPKCY";

  export default {
    components:{
    },
    data() {
      return {
        isReload:true,
        //1编辑，2添加，3预览
        showType:1,
        dialogVisible:true,
        lineCodes:[],
        selectedKeys:[],
        stationCodes:[],
        conductorPersonNames:[],//售票员名称
        operationCodes:[],
        formData:{
          dynamic: []
        },
        option:{
        }
    }
    },
    created() {
    },
    computed: {
      advanceAmountForm() {
        return {
          settleBeginDate: this.formData.settleBeginDate,
          settleEndDate: this.formData.settleEndDate,
          lineCode: this.formData.lineCode,
          stationCode: this.formData.stationCode
          //incomeTime: this.formData.incomeTime
        };
      }
    },
    methods: {
      handleSubmit(form,done){
        let param={};
        Object.assign(param,this.formData);
        this.formatterAmount(param,false);
        if(this.showType==2){
          addSwAuStationTicketDifference(param).then(res=>{
            if(res.data.code==0){
              this.$message.success("操作成功！");
              this.closed();
              this.$parent.getList();
            }else{
              this.$message.error("操作失败！");
            }
          }).catch((e)=>{
            this.$message.error("操作失败！");
          });
        }else if(this.showType==1){
          updateSwAuStationTicketDifference(param).then(res=>{
            if(res.data.code==0){
              this.$message.success("操作成功！");
              this.closed();
              this.$parent.getList();
            }else{
              this.$message.error("操作失败！");
            }
          }).catch((e)=>{
            this.$message.error("操作失败！");
          });
        }
        done();
      },
      async recallShow(row,showType){
          debugger
        if(this.formData.incomeTime=='' || !this.formData.incomeTime){
          this.formData.incomeTime = moment().format('YYYY-MM-DD');
        }
        this.showType=showType;
        this.getOption();
        if(showType!=2){ //编辑时
          Object.assign(this.formData,row);
          await this.changeLine2(row.lineCode);
          await this.changeStation(row.stationCode);
          await this.formatterAmount(this.formData,true);
        }
      },
      closed(){
        this.dialogVisible=false;
        this.$parent.isShowDiaAdd=false;
      },
      getOption(){
        let th=this;
        this.option=getCheZhanPKCYoption(th);
        this.isReload=false;
        this.$nextTick(()=>{
          this.isReload=true;
        })
      },
      async getSheBei(){
        let res=[];
        await getSwPmDeviceOperationCodeList({lineCode:this.formData.lineCode,stationCode:this.formData.stationCode}).then(r=>{
          res=r.data.data;
        });
        this.operationCodes=res;
        return res;
      },
      formatterAmount(row,isYuan){ //格式化钱
        if(isYuan){
          row.cashReceiptsAmount=fenExYuangettoFixed(row,'cashReceiptsAmount');
          row.cashPayAmount=fenExYuangettoFixed(row,'cashPayAmount');
          //row.stationReceiptsAmount=fenExYuangettoFixed(row,'stationReceiptsAmount');
          row.diffAmount=fenExYuangettoFixed(row,'diffAmount');
          row.shortAmount=fenExYuangettoFixed(row,'shortAmount');
        }else if(!isYuan){
          row.cashReceiptsAmount=Number(row.cashReceiptsAmount)*100;
          row.cashPayAmount=Number(row.cashPayAmount)*100;
          //row.stationReceiptsAmount=Number(row.stationReceiptsAmount)*100;
          row.diffAmount=Number(row.diffAmount)*100;
          row.shortAmount=Number(row.shortAmount)*100;
        }
        return row;
      },
      xianjinYingShouAllAmount(){
        xianjinYingShouAllAmount(this.formData).then(res=>{
          if(res.data.code==0){
            this.formData.cashReceiptsAmount=res.data.data;
          }

        })
      },
      xianjinYingShouAllAmounts(){
        let query = Object.assign({ size: -1 }, this.advanceAmountForm);
        if(
          !this.validatenull(this.advanceAmountForm.settleBeginDate) &&
          !this.validatenull(this.advanceAmountForm.settleEndDate) &&
          !this.validatenull(this.advanceAmountForm.lineCode) &&
          !this.validatenull(this.advanceAmountForm.stationCode)
        ){
          xianjinYingShouAllAmounts(query).then(res=>{
            if(res.data.code==0){
              this.formData.cashReceiptsAmount=res.data.data;  //车站营收日报
              //this.formData.diffAmount=(this.formData.cashReceiptsAmount+this.formData.shortAmount)-this.formData.cashPayAmount
	      this.formData.diffAmount=this.formData.cashPayAmount -this.formData.cashReceiptsAmount- this.formData.shortAmount
            }
          })
        }
      },
      changeLine(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
          this.formData.stationCode='';
        });
        this.xianjinYingShouAllAmounts();
      },
      changeLine2(v){ //改变线路时
        getgetStationCodes(v).then(res=>{
          this.stationCodes=res;
        });
        this.xianjinYingShouAllAmounts();
      },
      changeStation(v){
        let p = {
          line_code : this.formData.lineCode,
          station_code : v,
          belong_to_line:0
        };
        getSysUserToLS(p).then((res)=>{
          this.conductorPersonNames=res.data.data;
        });
        this.xianjinYingShouAllAmounts();
      }
    },
    mounted() {
      getLineCodes().then(res=>{ //获取线路
        this.lineCodes=res;
      });
    }
  }
</script>
<style lang="scss">
  .cheZhanPKCY{
    .el-form-item__label {
      width: 29% !important;
      padding-right: 0px;
    }
  }

</style>
